package com.jeeplus.test.jlnu.student.service.impl;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jeeplus.sys.utils.UserUtils;
import com.jeeplus.test.jlnu.admissionInfo.mapper.AdmissionInfoMapper;
import com.jeeplus.test.jlnu.student.domain.entity.StudentApply;
import com.jeeplus.test.jlnu.student.domain.entity.StudentStatus;
import com.jeeplus.test.jlnu.student.domain.result.StatusResult;
import com.jeeplus.test.jlnu.student.mapper.BkStudentInfoMapper;
import com.jeeplus.test.jlnu.student.mapper.StudentApplyMapper;
import com.jeeplus.test.jlnu.student.mapper.StudentStatusMapper;
import com.jeeplus.test.jlnu.student.service.StudentStatusService;
import com.jeeplus.test.jlnu.studentStatusInfo.mapper.StudentStatusInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class StudentStatusServiceImpl extends ServiceImpl<StudentStatusMapper, StudentStatus> implements StudentStatusService {
    @Autowired
    private StudentStatusMapper studentStatusMapper;
    @Autowired
    private StudentApplyMapper studentApplyMapper;
    @Autowired
    private AdmissionInfoMapper admissionInfoMapper;
    @Autowired
    private BkStudentInfoMapper bkStudentInfoMapper;

    @Autowired
    private StudentStatusInfoMapper studentStatusInfoMapper;


    @Override
    public Boolean studentStatusChange(StudentStatus studentStatus) {
        studentStatus.setId(IdUtil.simpleUUID());
        studentStatus.setFlag("0");
        studentStatusMapper.insert(studentStatus);
        StatusResult statusResults1 = studentStatusMapper.flimStatus1(studentStatus.getStudentId());
        StatusResult statusResults2 = studentStatusMapper.flimStatus2(studentStatus.getStudentId());
        StatusResult statusResults3 = studentStatusMapper.flimStatus3(studentStatus.getStudentId());
        StatusResult statusResults4 = studentStatusMapper.flimStatus4(studentStatus.getStudentId());
        List<StatusResult> statusResults = new ArrayList<>();
        if (ObjectUtil.isNotEmpty(statusResults1.getMessagePlan())) {
            statusResults.add(statusResults1);
        }
        if (ObjectUtil.isNotEmpty(statusResults2.getMessagePlan())) {
            statusResults.add(statusResults2);
        }
        if (ObjectUtil.isNotEmpty(statusResults3.getMessagePlan())) {
            statusResults.add(statusResults3);
        }
        if (ObjectUtil.isNotEmpty(statusResults4.getMessagePlan())) {
            statusResults.add(statusResults4);
        }
        for (StatusResult statusResult : statusResults) {
            StudentApply studentApply = new StudentApply();
            studentApply.setId(IdUtil.simpleUUID());
            studentApply.setStatusId(studentStatus.getId());
            studentApply.setReason(statusResult.getMessage());
            studentApply.setApplyTime(statusResult.getMessageTime());
            studentApply.setApplyType(statusResult.getMessagePlan());
            studentApply.setApplyAgo(statusResult.getMessageAgo());
            studentApply.setApplyNow(statusResult.getMessageNow());
            studentApply.setApplyStatus("0");
            studentApply.setTask("0");
            studentApply.setCreateTime(new Date());
            studentApply.setCreateUser(admissionInfoMapper.getNameById(statusResult.getStudentId()));
            studentApplyMapper.insert(studentApply);
            studentStatusMapper.updateFlag(studentApply.getStatusId());
        }
        return true;
    }

    @Override
    public Boolean studentStatusApply(String id, String status) {
        String updateUser = UserUtils.getCurrentUserDTO().getName();
        Date updateTime = new Date();

        StudentApply applyData = studentApplyMapper.selectById(id);
        String applyType = applyData.getApplyType();
        StudentStatus studentStatus = studentStatusMapper.selectById(applyData.getStatusId());//申请记录
        String studentId = studentStatus.getStudentId();
        String applyNow = applyData.getApplyNow();
        String type = findType(applyType);
        String key = UserUtils.getCurrentUserDTO().getRoleIdList().get(0);
        String key2 = UserUtils.getCurrentUserDTO().getOfficeDTO().getId();
        if ("1".equals(status)) {
            //如果通过 做 什么=操作
            if (key.equals("1854767206064549890") || key.equals("1854767309747744769")) {
                //只更新 流程变化 以及 审批人  审批时间
                studentApplyMapper.updateStatusById(id, updateUser, updateTime);
            }
            if (key2.equals("1854766833937510402")) {
                studentApplyMapper.updateStatusById2(id, updateUser, updateTime);
                //学院审批后才能改
                bkStudentInfoMapper.updateByType(type, applyNow, studentId);
                String addmission = findAddmission(applyType);
                if ("年级".equals(addmission)) {
                    studentStatusInfoMapper.updateAdmission("grade", applyNow, studentId);
                } else if ("班号".equals(addmission)) {
                    studentStatusInfoMapper.updateAdmission("class_id", applyNow, studentId);
                }
                admissionInfoMapper.updateAdmission(addmission, applyNow, studentId);
            }
        } else if ("2".equals(status)) {
            if (key.equals("1854767206064549890") || key.equals("1854767309747744769")) {
                //学生驳回
                studentApplyMapper.updateStatusById3(id, updateUser, updateTime);
            }
            if (key2.equals("1854766833937510402")) {
                //学院驳回
                studentApplyMapper.updateStatusById4(id, updateUser, updateTime);
            }
        }

        return true;
    }

    public String findType(String applyType) {
        String result = "";

        if (applyType.contains("学院名称")) {
            result = "college_name";
        } else if (applyType.contains("系科")) {
            result = "department";
        } else if (applyType.contains("专业名称")) {
            result = "major_name";
        } else if (applyType.contains("专业代码")) {
            result = "major_code";
        } else if (applyType.contains("层次")) {
            result = "level";
        } else if (applyType.contains("学制")) {
            result = "education_system";
        } else if (applyType.contains("年级")) {
            result = "grade";
        } else if (applyType.contains("班号")) {
            result = "class_number";
        } else if (applyType.contains("姓名")) {
            result = "name";
        } else if (applyType.contains("曾用名")) {
            result = "former_name";
        } else if (applyType.contains("民族")) {
            result = "ethnicity";
        } else if (applyType.contains("出生日期")) {
            result = "birth_date";
        } else if (applyType.contains("身份证号码")) {
            result = "id_card_number";
        }

        return result;
    }

    public String findAddmission(String type) {
        String result = "";
        if (type.contains("学院名称")) {
            result = "admission_academy";
        } else if (type.contains("系科")) {
            result = "";
        } else if (type.contains("专业名称")) {
            result = "admission_major";
        } else if (type.contains("专业代码")) {
            result = "";
        } else if (type.contains("层次")) {
            result = "gradation";
        } else if (type.contains("学制")) {
            result = "educational_system";
        } else if (type.contains("姓名")) {
            result = "name";
        } else if (type.contains("曾用名")) {
            result = "";
        } else if (type.contains("民族")) {
            result = "nation";
        } else if (type.contains("出生日期")) {
            result = "birthday";
        } else if (type.contains("身份证号码")) {
            result = "id_card";
        }
        return result;
    }


}
